package common;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import db.DBManager;

public class Receipt {
	
	private String c_code = null;			//顧客コード
	private String c_name = null;			//顧客名
	private String c_post = null;			//顧客郵便番号
	private String c_address = null;		//顧客住所
	private String c_tel = null;				//顧客電話番号
	private String r_num =null;			//納品番号
	private String o_num = null;			//受注番号
	private Date today = null;				//納品書作成日
	private Date deliver = null;				//配達予定日
 	private String i_code = null;			//商品コード
 	private String i_name = null;			//商品名
 	private String i_size = null;				//商品規格
 	private int i_cost =0;					//原単価
 	private int i_price = 0;					//商品単価
 	private int oders = 0;						//注文数
 	private int price_h_sum = 0;			//金額横計（行単位）
 	private int price_total = 0;				//合計金額（商品のみ）
 	private int fee = 0;						//配送料
 	private int total_pay = 0;				//合計請求金額
 	
 	/**
	 * 納品書発行未処理の受注一覧を取得する。
	 * @return	未処理の受注情報の配列
	 */
	public static List<Receipt> receipt()
	{
		Connection con = DBManager.getConnection();
		List<Receipt> list = new ArrayList<Receipt>();
		try
		{
			String sql = "select * from receipt where paid is null or paid = 0 order by num";
			Statement stm = con.createStatement();
			ResultSet rs = stm.executeQuery(sql);
			while(rs.next())
			{
				Receipt r = new Receipt();
				r.setR_num(rs.getString("num"));								//納品番号
				r.setO_num(rs.getString("order_num"));						//受注番号
				r.setC_code(rs.getString("customer_code"));				//顧客コード(お客様コード)
				r.setPrice_total(rs.getInt("price_total"));						//合計金額
				r.setFee(rs.getInt("fee"));											//配送料
				r.setDeliver(rs.getDate("deliver")); 							//配達希望日
				r.setToday(rs.getDate("date"));									//納品書作成日
				list.add(r);
			}
			rs.close();
			stm.close();
			con.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return list;
	}

	public List<ReceiptItem> receipt_item()
	{
		Connection con = DBManager.getConnection();
		List<ReceiptItem> items = new ArrayList<ReceiptItem>();
		try
		{
			String sql = "select * from receipt_item where receipt_num = '" + this.r_num +  "' order by num";
			Statement stm = con.createStatement();
			ResultSet rs = stm.executeQuery(sql);
			while(rs.next())
			{
				ReceiptItem r = new ReceiptItem();
			//	r.setI_code(rs.getString("receipt_num"));					//納品番号
			//	r.setO_num(rs.getString("order_num"));						//受注番号
				r.setItem_code(rs.getString("item_code"));						//商品コード
			//	r.setI_name(rs.getString(columnIndex));.setI_price(i_price);.setCost(rs.getInt("cost"));										//商品名がほしい
				r.setPrice(rs.getInt("price"));									//売単価
				r.setOrders(rs.getInt("orders")); 								//注文数
				items.add(r);
			}
			rs.close();
			stm.close();
			con.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return items;
	}

	/**
	 * カート情報を受注明細テーブルと受注情報テーブルに登録する
	 * @param cart
	 * @return	受注番号
	 * @throws SQLException
	 */
	/*
	public static String  add(Cart cart)
	{
		Connection con = DBManager.getConnection();
		List<OrderItem> list = cart.getItems();
	int cost_total = 0;
//		int price_total = 0;
		try
		{
			Statement smt = con.createStatement();
			for( int i=0; i < list.size(); i++)
			{
				OrderItem o = list.get(i);
				Item item = Item.find_by_code(o.getItem_code());
				String sqlItem = "insert into order_item"
						+ "(order_num,num,item_code,cost,price,orders)"
						+ "values ( "
						+ cart.getOrder_num()+","		//受注番号
						+ (i + 1) + ","								//行番号
						+ o.getItem_code() +","			//商品番号
						+ item.getCost() +","					//原価
						+ item.getPrice() +","					//売価
						+ o.getOrders() +""					//注文数
						+ ")";
				smt.executeUpdate(sqlItem);
				cost_total = cost_total + item.getCost();
//				price_total = price_total + item.getPrice();
			}
//			int fee = 500;
//			if(price_total >= 10000) fee = 0;
			OrderItem o = list.get(order_num);

			String sqlInfo = "insert into receipt"
					+ "(num,order_num,customer_code,date,cost_total,price_total,fee,deliver,paid) "
					+ " values ("
					+ o.getOrder_num()+","													//受注番号
					+ o.getCs().getCode()+","													//顧客番号
					+ new java.sql.Date(System.currentTimeMillis()) +","			//受注日
					+ cost_total+","																		//合計原価
					+ cart.getPrice_total()+","														//合計売価
					+ cart.getFee()+","																	//運賃
					+ cart.getDeli()+","																//配達予定日
					+ cart.getJudge()+","																//与信結果
					+ "0"																						//納品書発行フラグ
					+ ")"
			
					+ "insert into receipt_item"
					+ "(receipt_num,num,item_code,cost,price,orders)"
					+ "values ("
					+ ")";
			smt.executeUpdate(sqlInfo);
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return cart.getOrder_num();
	}
	*/
 	
	public String getC_code() {
		return c_code;
	}
	public void setC_code(String c_code) {
		this.c_code = c_code;
	}
	public String getC_name() {
		return c_name;
	}
	public void setC_name(String c_name) {
		this.c_name = c_name;
	}
	public String getC_post() {
		return c_post;
	}
	public void setC_post(String c_post) {
		this.c_post = c_post;
	}
	public String getC_address() {
		return c_address;
	}
	public void setC_address(String c_address) {
		this.c_address = c_address;
	}
	public String getC_tel() {
		return c_tel;
	}
	public void setC_tel(String c_tel) {
		this.c_tel = c_tel;
	}
	public String getR_num() {
		return r_num;
	}
	public void setR_num(String r_num) {
		this.r_num = r_num;
	}
	public String getO_num() {
		return o_num;
	}
	public void setO_num(String o_num) {
		this.o_num = o_num;
	}
	public String getWareki(){
		return String.format("%tY-%tm-%td", this.today,this.today,this.today);
	}
	public Date getToday() {
		return today;
	}
	public void setToday(Date today) {
		this.today = today;
	}
	public Date getDeliver() {
		return deliver;
	}
	public void setDeliver(Date deliver) {
		this.deliver = deliver;
	}
	public String getI_code() {
		return i_code;
	}
	public void setI_code(String i_code) {
		this.i_code = i_code;
	}
	public String getI_name() {
		return i_name;
	}
	public void setI_name(String i_name) {
		this.i_name = i_name;
	}
	public String getI_size() {
		return i_size;
	}
	public void setI_size(String i_size) {
		this.i_size = i_size;
	}
	public int getI_cost() {
		return i_cost;
	}
	public void setI_cost(int i_cost) {
		this.i_cost = i_cost;
	}
	public int getI_price() {
		return i_price;
	}
	public void setI_price(int i_price) {
		this.i_price = i_price;
	}
	public int getOders() {
		return oders;
	}
	public void setOders(int oders) {
		this.oders = oders;
	}
	public int getPrice_h_sum() {
		return price_h_sum;
	}
	public void setPrice_h_sum(int price_h_sum) {
		this.price_h_sum = price_h_sum;
	}
	public int getPrice_total() {
		return price_total;
	}
	public void setPrice_total(int price_total) {
		this.price_total = price_total;
	}
	public int getFee() {
		return fee;
	}
	public void setFee(int fee) {
		this.fee = fee;
	}
	public int getTotal_pay() {
		return total_pay;
	}
	public void setTotal_pay(int total_pay) {
		this.total_pay = total_pay;
	}

 	
}
